package io.github.topcatv.mall.common.controller;

import com.qiniu.util.Auth;
import io.github.topcatv.mall.common.ao.ReturnObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * @author liuyi
 */
@RestController
@RequestMapping("/common")
public class CommonController {
    private Auth auth;
    @Value("${qiniu.ak}")
    private String accessKey;
    @Value("${qiniu.sk}")
    private String secretKey;
    @Value("${qiniu.bucket}")
    private String bucket;

    @GetMapping("/uploadParams")
    public ReturnObject qiNiuToken() {
        if (auth == null) {
            auth = Auth.create(accessKey, secretKey);
        }
        String key = UUID.randomUUID().toString();
        String token = auth.uploadToken(bucket, key);
        return ReturnObject.success().put("token", token).put("key", key);
    }
}
